// 存储变量名
const storageKey = 'storage_data'

// 存储的数据
export function storageData () {
	return localStorage.getItem(storageKey) || {}
}

export const storage = {
  set: function(key:string, value: any) {
    let tmp = localStorage.getItem(storageKey)
    tmp = tmp ? JSON.parse(tmp) : {}
    tmp[key] = value
    
    localStorage.setItem(storageKey, JSON.stringify(tmp))
	  console.log(storageKey, '设置成功', storageData())
  },
  get: function(key: string | number) {
    return (JSON.parse(storageData())[key]) || ""
  },
  remove: function(key) {
    let tem = storageData();
		delete tem[key];
		localStorage.setItem(storageKey, tem);
  },
  clean: function() {
    localStorage.removeItem(storageKey)
  }
}






/**
 *@description  优化高频率调用函数的方式
 *@param func 函数名
 *@param delay 时间 ms
 * **/
// eslint-disable-next-line no-unused-vars
export function debounce<F extends (...args: any[]) => void>(func: F, delay: number): (...args: Parameters<F>) => void {
  let timerId: NodeJS.Timeout | null;

  return function (this: any, ...args: Parameters<F>): void {
    if (timerId) {
      clearTimeout(timerId);
    }

    timerId = setTimeout(() => {
      func.apply(this, args);
    }, delay);
  };
}


